package com.kecheng.action;

import java.util.ArrayList;
import java.util.List;

import com.kecheng.dao.PeizhiDao;
import net.sf.json.JSONObject;

import com.kecheng.pojo.MainBtn;
import com.kecheng.pojo.Peizhi;
import com.kecheng.util.CreateTags;

import com.kecheng.com.yx.pojo.AccessToken;
import com.kecheng.com.yx.pojo.Button;
import com.kecheng.com.yx.pojo.CommonButton;
import com.kecheng.com.yx.pojo.ComplexButton;
import com.kecheng.com.yx.pojo.MatchRule;
import com.kecheng.com.yx.pojo.Menu;
import com.kecheng.com.yx.util.WeixinUtil;

import com.kecheng.dao.CommonDao;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

@Action(value = "menu", results = {
		@Result(name = "success", location = "/admin/weixin/weixin.jsp"),
})
@AllowedMethods(value = {("regex:.*")})
public class MenuAction {

	 private List<CommonButton> btn1;//二级菜单的name、type、key、url
	 private List<CommonButton> btn2;
	 private List<CommonButton> btn3;
	 
	 private ComplexButton complexButton;
	 private ComplexButton Button1;
	 private ComplexButton Button2;
	 private ComplexButton Button3;
	 private List<ComplexButton> ones;
	 
	 private MainBtn mainBtn1;//一级菜单(名称和分类)
	 private MainBtn mainBtn2;
	 private MainBtn mainBtn3;
	 @Autowired
	 private PeizhiDao peizhiDao;
	 private Peizhi peizhi;
	 private String msg;
	 private CommonDao dao;
	 private String tagName;
	 
	 /**
	  * 为用户批量打上标签test
	  */
	 public void TagToTeam(){
		List<String> openidList = null;
		Integer tagId = 103;
		openidList.add("oFwuT0a3EIj7Nx2YuZOR4kEB1QF8");
		CreateTags.addTagToTeams(openidList,tagId);
		
	 }
	 /**
	  * 创建标签，先放在这test
	  */
	 public void createTag(){
		 CreateTags.create(tagName);
	 }
	 
	public String getTagName() {
		return tagName;
	}
	public void setTagName(String tagName) {
		this.tagName = tagName;
	}



	//查询菜单的接口
	public void findMenu(){
		
		peizhi = peizhiDao.find();
		String appId = peizhi.getAppid();
		String appSecret = peizhi.getAppsecret();
		AccessToken at = WeixinUtil.getAccessToken(); 
		String result = WeixinUtil.findMenu(at.getToken()); 
		System.err.println("result："+result);
	}
	//跳转自定义菜单
	public String find(){
		ones = (List<ComplexButton>) dao.find(complexButton);
		CommonButton two = new CommonButton();
		for(ComplexButton c:ones){
			two.setComplexButtonId(c.getId());
			List<CommonButton> twos = (List<CommonButton>) dao.find(two);
			c.setSub_button(twos);
		}
		return "success";
	}
	//创建菜单
	public String add(){
		//创建菜单先把数据库的菜单清空
		List<ComplexButton>ones = (List<ComplexButton>) dao.find(complexButton);
		if(ones!=null){
			for(ComplexButton one:ones){
				CommonButton ccc = new CommonButton();
				ccc.setComplexButtonId(one.getId());
				List<CommonButton>twos = (List<CommonButton>) dao.find(ccc);
				if(twos!=null&&twos.size()>0){
					dao.dedete(twos);
				}
			}
		}
		if(ones!=null && ones.size()>0){
			dao.dedete(ones);
		}
		//一级菜单如果type为空，则表示有子菜单
	    List<Button> button = new ArrayList<Button>();
	    if(mainBtn1!=null){
	    	Button1=new ComplexButton();  
	    	Button1.setName(mainBtn1.getName());
	    	Button1.setClassify(mainBtn1.getClassify());
	    	Button1.setTagId(complexButton.getTagId());
	    	if(mainBtn1.getClassify()==1){//一级菜单
	    		Button1.setType(mainBtn1.getType());
	    		if("click".equals(mainBtn1.getType())){//事件click
	    			Button1.setKey0(mainBtn1.getUrl());
	    		}else{//链接view
	    			Button1.setUrl(mainBtn1.getUrl());
	    		}
	    	}
	    	System.err.println("name:"+Button1.getName());
	    	dao.add(Button1);
	    	//System.err.println("id:"+Button1.getId());
	    	if(mainBtn1.getClassify()==2){//二级菜单
	    		if(btn1!=null){
	    			for(CommonButton c:btn1){
	    				if("click".equals(c.getType())){
	    					c.setKey0(c.getUrl());
	    					c.setUrl("");
	    				}
	    				c.setComplexButtonId(Button1.getId());
	    				dao.add(c);
	    			}
	    			
	    		}
	    		Button1.setSub_button(btn1); 
	    	}
	    	button.add(Button1);
	    }
	      
         if(mainBtn2!=null){
        	 Button2=new ComplexButton();
        	 Button2.setName(mainBtn2.getName());
        	 Button2.setClassify(mainBtn2.getClassify());
        	 Button2.setTagId(complexButton.getTagId());
        	 if(mainBtn2.getClassify()==1){
 	    		Button2.setType(mainBtn2.getType());
 	    		if("click".equals(mainBtn2.getType())){
 	    			Button2.setKey0(mainBtn2.getUrl());
 	    		}else{
 	    			Button2.setUrl(mainBtn2.getUrl());
 	    		}
 	    	}
        	dao.add(Button2);
 	    	if(mainBtn2.getClassify()==2){
 	    		if(btn2!=null){
 	    			for(CommonButton c:btn2){
 	    				if("click".equals(c.getType())){
 	    					c.setKey0(c.getUrl());
 	    					c.setUrl("");
 	    				}
 	    				System.err.println(c.getType());
 	    				c.setComplexButtonId(Button2.getId());
	    				dao.add(c);
 	    			}
 	    			
 	    		}
 	    		Button2.setSub_button(btn2);
 	    	}
	    	 button.add(Button2);
	    	 
	      }
         if(mainBtn3!=null){
        	 Button3=new ComplexButton();
        	 Button3.setName(mainBtn3.getName()); 
        	 Button3.setClassify(mainBtn3.getClassify());
        	 Button3.setTagId(complexButton.getTagId());
        	 if(mainBtn3.getClassify()==1){
  	    		Button3.setType(mainBtn3.getType());
  	    		if("click".equals(mainBtn3.getType())){
  	    			Button3.setKey0(mainBtn3.getUrl());
  	    		}else{
  	    			Button3.setUrl(mainBtn3.getUrl());
  	    		}
  	    	}
        	dao.add(Button3);
  	    	if(mainBtn3.getClassify()==2){
  	    		if(btn3!=null){
  	    			for(CommonButton c:btn3){
  	    				if("click".equals(c.getType())){
  	    					c.setKey0(c.getUrl());
  	    					c.setUrl("");
  	    				}
  	    				c.setComplexButtonId(Button3.getId());
	    				dao.add(c);
  	    			}
  	    			
  	    		}
  	    		Button3.setSub_button(btn3); 
  	    	}
	    	button.add(Button3);
	    	
         }
         
        
		peizhi=peizhiDao.find();
		String appId = peizhi.getAppid();  
        // 第三方用户唯一凭证密钥   
		
        String appSecret = peizhi.getAppsecret();
        if(peizhi.getAppsecret()==null||peizhi.getAppid()==null){
        	msg = "null";
        	return "success";
        }
        // 调用接口获取access_token   
       AccessToken at = WeixinUtil.getAccessToken();  
       String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", at.getToken());
       Menu menu = new Menu();  
       menu.setButton(button); 
       if(complexButton!=null&&complexButton.getTagId()!=0){
    	   url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", at.getToken());	   
    	   MatchRule matchrule = new MatchRule();
    	   matchrule.setTag_id(complexButton.getTagId());
    	   //matchrule.setLanguage("zh_CN");
    	   menu.setMatchrule(matchrule);
       }
           
        System.err.println(url);
        if (null != at) {  
            // 调用接口创建菜单   
            JSONObject jsonObject = WeixinUtil.createMenu(menu,url);  
            System.out.println("返回jsonObject:"+jsonObject);
            if(jsonObject!=null){
            	if(url.contains("addconditional")){
            		//{"menuid":471244700}
            		//int menuid = jsonObject.getInt("menuid");
            		
            	}else{
            		int result = 0;
            		if (null != jsonObject) {
            			if (0 != jsonObject.getInt("errcode")) {
            				result = jsonObject.getInt("errcode");
            			}
            		}
//            // 判断菜单创建结果  
            		if (0 == result) { 
            			System.out.println("菜单创建成功！");
            			msg="success";
            		}else { 
            			System.out.println("菜单创建失败，错误码：" + result); 
            			
            			msg="fail";
            		}
            	}
            }
        }  
		return find();
	}
	
	
	
	public ComplexButton getButton1() {
		return Button1;
	}

	public void setButton1(ComplexButton button1) {
		Button1 = button1;
	}

	public ComplexButton getButton2() {
		return Button2;
	}

	public void setButton2(ComplexButton button2) {
		Button2 = button2;
	}

	public ComplexButton getButton3() {
		return Button3;
	}

	public void setButton3(ComplexButton button3) {
		Button3 = button3;
	}

	public List<CommonButton> getBtn1() {
		return btn1;
	}

	public void setBtn1(List<CommonButton> btn1) {
		this.btn1 = btn1;
	}


	public List<CommonButton> getBtn2() {
		return btn2;
	}

	public void setBtn2(List<CommonButton> btn2) {
		this.btn2 = btn2;
	}

	public List<CommonButton> getBtn3() {
		return btn3;
	}

	public void setBtn3(List<CommonButton> btn3) {
		this.btn3 = btn3;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Peizhi getPeizhi() {
		return peizhi;
	}

	public void setPeizhi(Peizhi peizhi) {
		this.peizhi = peizhi;
	}

	public MainBtn getMainBtn1() {
		return mainBtn1;
	}

	public void setMainBtn1(MainBtn mainBtn1) {
		this.mainBtn1 = mainBtn1;
	}

	public MainBtn getMainBtn2() {
		return mainBtn2;
	}

	public void setMainBtn2(MainBtn mainBtn2) {
		this.mainBtn2 = mainBtn2;
	}

	public MainBtn getMainBtn3() {
		return mainBtn3;
	}

	public void setMainBtn3(MainBtn mainBtn3) {
		this.mainBtn3 = mainBtn3;
	}
	public CommonDao getDao() {
		return dao;
	}
	public void setDao(CommonDao dao) {
		this.dao = dao;
	}
	public List<ComplexButton> getOnes() {
		return ones;
	}
	public void setOnes(List<ComplexButton> ones) {
		this.ones = ones;
	}
	public ComplexButton getComplexButton() {
		return complexButton;
	}
	public void setComplexButton(ComplexButton complexButton) {
		this.complexButton = complexButton;
	}
	
}
